package controller.impl;

import java.util.ArrayList;
import java.util.List;
import util.page.PageBean;
import controller.services.IArticleServices;
import model.bo.Article;
import model.bo.Articletype;
import model.bo.SubTree;
import model.dao.IArticleDAO;
import model.dao.IArticletypeDAO;

public class ArticleServices implements IArticleServices {
	
	private IArticleDAO IArticleDAO;
	private IArticletypeDAO IArticletypeDAO;
	
	//通过类型查找
	public PageBean articlelist(Object typeid,int pageSize,int page) {
		String hql = "from Article as model where model.typeid=="+typeid;
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	public PageBean articlelist(String typeid,String condetions,int pageSize,int page){
		String hql = "from Article as model where model.typeid=="+typeid+" and model.title like '%"+condetions+"%'";
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	//保存文章
	public Boolean save(Article transientInstance){
		try{
			IArticleDAO.save(transientInstance);
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return false;
	}
	
	//执行SQL删除
	public Boolean delete(String arr) {
		try{
			String articleArr = arr;
			String sql = "delete from article where articleid in ("+articleArr+")";
			IArticleDAO.execusql(sql);
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	public List<SubTree> getTypetree() {
		List<Articletype> allType = IArticletypeDAO.findAll();
		List<SubTree> result = new ArrayList<SubTree>();
		//把根目录填充节点
		for(int i=0;i<allType.size();i++){
			//判断父子分类
			if(allType.get(i).getFatherid()==0){
				List<Articletype> _temp_ = new ArrayList<Articletype>();
				//定义变量保存对象
				Articletype artype = allType.get(i);
				//保存父ID
				int fid = artype.getId();
				SubTree tree = new SubTree();
				tree.setId(artype.getId());
				tree.setNodeName(artype.getTypename());
				//通过父ID去匹配子类ID；
				for(int j=0;j<allType.size();j++){
					if(fid==allType.get(j).getFatherid()){
						Articletype childNode = (Articletype)allType.get(j);
						_temp_.add(childNode);
					}
				}
				//初始化节点对象
				tree.setHasChild(_temp_);
				//添加进入节点
				result.add(tree);
			}
		}
		return result;
	}
	
	public Article findById(Integer id) {
		// TODO Auto-generated method stub
		return IArticleDAO.findById(id);
	}

	public PageBean findByTypeid(Object typeid, int pageSize, int page) {
		//TODO Auto-generated method stub
		String hql = "from Article as model where model.typeid = "+typeid+" order by model.articletime desc";
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	
	//查詢一個欄目下的所有文章
	public PageBean findByFatherid(Object fatherid, int pageSize, int page) {
		//TODO Auto-generated method stub
		String hql = "from Article as model where model.fathertypeid = "+fatherid+" order by model.articletime desc";
		int allRow = IArticleDAO.getAllRowCount(hql);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);    //总页数
        final int offset = PageBean.countOffset(pageSize, page);    //当前页开始记录
        final int length = pageSize;    //每页记录数
        final int currentPage = PageBean.countCurrentPage(page);
        List articlelist = IArticleDAO.queryForPage(hql,offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);    
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(articlelist);
        pageBean.init();
        return pageBean;
	}
	public IArticleDAO getIArticleDAO() {
		return IArticleDAO;
	}

	public void setIArticleDAO(IArticleDAO articleDAO) {
		IArticleDAO = articleDAO;
	}

	public IArticletypeDAO getIArticletypeDAO() {
		return IArticletypeDAO;
	}

	public void setIArticletypeDAO(IArticletypeDAO articletypeDAO) {
		IArticletypeDAO = articletypeDAO;
	}
	//通过属性查找
	public List findByProperty(String propertyName, Object value) {
		// TODO Auto-generated method stub
		return IArticleDAO.findByProperty(propertyName, value);
	}
}
